package com.dinadi.yidi.ui.activity;

import android.content.Intent;
import android.os.Handler;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.adapter.DailyExhibitionAdapter;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.bean.CreativeProductBean;
import com.dinadi.yidi.bean.CreativeProductRequest;
import com.dinadi.yidi.bean.CreativeProductRequestEntity;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.utils.ImageUtils;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.UtilPopupWindow;
import com.dinadi.yidi.utils.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/4/6    展览
 * Time: 14:34
 */

public class WorksExhibitionActivity extends BaseActivity {
    private View view;
    private TextView tvTitle;
    private PullLoadMoreRecyclerView mPullLoadMoreRecyclerView;
    private DailyExhibitionAdapter mRefreshAdapter;
    private List<CreativeProductRequestEntity> dataList = new ArrayList<>();
    private int page = 1;
    private CreativeProductRequest beanRequest;
    private boolean isShow;
    private String showId;

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(WorksExhibitionActivity.this).inflate(R.layout.activity_daily_exhibition, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {
        changePager(2);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        tvTitle.setText(getResources().getString(R.string.text_daily_exhibition));
        mPullLoadMoreRecyclerView = (PullLoadMoreRecyclerView) view.findViewById(R.id.pullLoadMoreRecyclerView);
        mPullLoadMoreRecyclerView.setLinearLayout();
        mRefreshAdapter = new DailyExhibitionAdapter(WorksExhibitionActivity.this);
        mPullLoadMoreRecyclerView.setAdapter(mRefreshAdapter);
        mPullLoadMoreRecyclerView.setOnPullLoadMoreListener(new PullLoadMoreListener());
        isShow = true;
    }


    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {


        Intent intent = getIntent();
        showId = intent.getStringExtra("showId");
        showLoadingDialog(getResources().getString(R.string.dialog_text));
        initPullrefresh();
    }


    @Override
    protected void clickEvent(View v) {

    }

    HttpListener httpListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                closeLoadingDialog();
                beanRequest = JsonUtil.parseJsonToBean(response, CreativeProductRequest.class);
                dataList = beanRequest.getProductList();
                if (isShow == true) {
                    showExhibitionDialog();
                } else {

                }
                mRefreshAdapter.addAllData(dataList);
                getData();


            } else {
                closeLoadingDialog();
                toast(errMsg);
            }


        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            closeLoadingDialog();
            toast("加载失败");
        }
    };

    private void getData() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新结束
                mPullLoadMoreRecyclerView.setPullLoadMoreCompleted();
            }
        }, 1000);
    }

    class PullLoadMoreListener implements PullLoadMoreRecyclerView.PullLoadMoreListener {
        @Override
        public void onRefresh() {
            setRefresh();
        }

        @Override
        public void onLoadMore() {
            page = page + 1;
            initPullrefresh();
        }
    }

    private void setRefresh() {
        mRefreshAdapter.clearData();
        page = 1;
        initPullrefresh();
    }

    private void initPullrefresh() {
        CreativeProductBean bean = new CreativeProductBean();
        bean.setToken("");
        bean.setShowId(showId);
        bean.setPageNum(page + "");
        bean.setPageSize("5");
        CommonInterface creativeProductInterface = new CommonInterface(WorksExhibitionActivity.this, httpListener);
        creativeProductInterface.getInterface("12002", JSON.toJSONString(bean));

    }

    private void showExhibitionDialog() {
        //当然是先初始化popupWindow
        final UtilPopupWindow popupWindow = new UtilPopupWindow();
        //我再工具中没有写构造函数，这里调用默认的构造函数
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        final int windowHeight = d.getHeight();
        //弹出框填充布局
        LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.popupwindow_exhibition, null);
        RelativeLayout layout2 = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_daily_exhibition, null);
        popupWindow.setPopupWindow(WorksExhibitionActivity.this, windowHeight, layout, layout2, Gravity.CENTER);
        ImageView cover_img = (ImageView) layout.findViewById(R.id.cover_img);
        TextView text_desc = (TextView) layout.findViewById(R.id.text_desc);
        String cover_imgUrl = beanRequest.getCoverImg();
        String desc = beanRequest.getDesc();

        if (!cover_imgUrl.equals("")) {
            ImageUtils.loadImageView(WorksExhibitionActivity.this, cover_imgUrl, cover_img);
        } else {
            ImageUtils.loadImageView(WorksExhibitionActivity.this, R.drawable.me_img_default, cover_img);
        }
        text_desc.setText(desc);
        ImageView dimss_pupopwindow = (ImageView) layout.findViewById(R.id.dimss_pupopwindow);
        dimss_pupopwindow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                isShow = false;
            }
        });
    }


}

